package com.yzkj.cpjxc.business.service.impl;

import com.yzkj.cpjxc.business.framework.common.BusinessMap;
import com.yzkj.cpjxc.business.framework.common.Constants;
import com.yzkj.cpjxc.business.framework.common.Pager;
import com.yzkj.cpjxc.business.framework.utils.IDUtils;
import com.yzkj.cpjxc.business.domain.JieZhang;
import com.yzkj.cpjxc.business.service.JieZhangService;
import com.yzkj.cpjxc.business.domain.ZhangTao;
import com.yzkj.cpjxc.business.domain.ZhangTaoQueryVo;
import com.yzkj.cpjxc.business.service.ZhangTaoService;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 账套 Service 实现
 */
@Service
public class ZhangTaoServiceImpl implements ZhangTaoService {

	/**
	 * MyBatis 命名空间
	 */
	private final static String MAPPER_NAMESPACE = "com.chkj.xezz.zhangTao.mappers.ZhangTaoMapper";

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	@Autowired
	private JieZhangService jieZhangService;
	
	@Override
	public BusinessMap create(ZhangTao zhangTao) throws Exception {
		String uuid = zhangTao.getUuid();
		zhangTao.setUuid(null);
		BusinessMap bm = new BusinessMap();
		if (this.isExists(zhangTao)) {
			bm.put(Constants.WARNING_MSG, "该账套名称已经存在");
			return bm;
		}
		zhangTao.setUuid(uuid);
		sqlSessionTemplate.insert(MAPPER_NAMESPACE + ".add", zhangTao);
		
		//添加当前账套做账期数
		JieZhang jieZhang = new JieZhang();
		jieZhang.setUuid(IDUtils.genUUID());
		jieZhang.setZhangTaoId(uuid);
		jieZhang.setJzYear(zhangTao.getQyYear());
		jieZhang.setJzMonth(zhangTao.getQyMonth());
		jieZhang.setIsYjz(Constants.IS_WJZ);
		jieZhangService.create(jieZhang);
		
		return bm;
	}

	@Override
	public boolean isExists(ZhangTao zhangTao) throws Exception {
		return (Integer)sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".isExists", zhangTao) > 0;
	}
	
	@Override
	public ZhangTao getById(String uuid) throws Exception {
		return sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getById", uuid);
	}
	
	@Override
	public BusinessMap edit(ZhangTao zhangTao) throws Exception {
		BusinessMap bm = new BusinessMap();
		if (this.isExists(zhangTao)) {
			bm.put(Constants.WARNING_MSG, "该账套名称已经存在");
			return bm;
		}
		sqlSessionTemplate.update(MAPPER_NAMESPACE + ".edit", zhangTao);
		return bm;
	}

	@Override
	public boolean delete(String uuid) throws Exception {
		sqlSessionTemplate.delete(MAPPER_NAMESPACE + ".deleteById", uuid);
		return true;
	}

	@Override
	public Pager<ZhangTao> search(Pager<ZhangTao> pager, ZhangTaoQueryVo queryVo) throws Exception {
		List<ZhangTao> dataList = sqlSessionTemplate.selectList(MAPPER_NAMESPACE + ".page", queryVo, new RowBounds((pager.getCurrentPage() - 1) * pager.getLimit(), pager.getLimit()));
		pager.setDataList(dataList);
		
		// 如果需要转换成ShowVo展示，那么就在这里转换
		
		Long total = sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getTotal", queryVo);
		pager.setTotal(total);
		return pager;
	}

}
